package com.rong.text;
import com.rong.dao.IUserInfoMapper;
import com.rong.pojo.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Text01 {

    public static void main(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session= sqlSessionFactory.openSession();
            IUserInfoMapper mapper= session.getMapper(IUserInfoMapper.class);
            int num=  mapper.deleteUserInfo(4);
            System.out.println(num);
//            手动提交事务
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void test05(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session = sqlSessionFactory.openSession();
            IUserInfoMapper mapper = session.getMapper(IUserInfoMapper.class);
            UserInfo userInfo = new UserInfo();
            userInfo.setId(4);
            userInfo.setUsername("赵四");
//            userInfo.setUseraddress("北京");
//            userInfo.setUserTel("1234567897");
//            userInfo.setUserpwd("123456");
//            userInfo.setIsdelete(0);
//           调用新增方法，将输入添加到数据库中
            int num = mapper.updateUser(userInfo);
            System.out.println(num);
//            手动提交事务
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void test04(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session= sqlSessionFactory.openSession();
            IUserInfoMapper mapper= session.getMapper(IUserInfoMapper.class);
            UserInfo userInfo=new UserInfo();
            userInfo.setId(4);
            userInfo.setUsername("李四");
            userInfo.setUseraddress("北京");
            userInfo.setUserTel("1234567897");
            userInfo.setUserpwd("123456");
            userInfo.setIsdelete(0);
//           调用新增方法，将输入添加到数据库中
            int num=  mapper.insertUserInfo(userInfo);
            System.out.println(num);
//            手动提交事务
            session.commit();



        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public static void text03(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session= sqlSessionFactory.openSession();
            IUserInfoMapper mapper=session.getMapper(IUserInfoMapper.class);
            List<UserInfo> user= mapper.selectAllUserInfo();
            for (UserInfo u:user
            ) {
                System.out.println(u);
            }




        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public static void test02(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
            //           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session= sqlSessionFactory.openSession();
            //使用接口方式查询
            IUserInfoMapper mapper=session.getMapper(IUserInfoMapper.class);
            UserInfo user= mapper.selectUserinfoByid(1);
            System.out.println(user);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void test01(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
            //           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session = sqlSessionFactory.openSession();
            UserInfo userInfo = session.selectOne("com.rong.dao.userinfoMapper.selectUserinfoByid", 1);
            System.out.println(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}